package First;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class LoginPage extends JFrame {

    private JTextField idField;
    private JPasswordField passwordField;

    public LoginPage() {
        createView();

        setTitle("学生管理系统 - 登录页面");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 200);
        setLocationRelativeTo(null);
        setResizable(false);
    }

    private void createView() {
        JPanel panel = new JPanel(new GridBagLayout());
        getContentPane().add(panel);

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(10, 10, 10, 10);

        // Id
        JLabel idLabel = new JLabel("学号: ");
        constraints.gridx = 0;
        constraints.gridy = 0;
        panel.add(idLabel, constraints);

        idField = new JTextField(20);
        constraints.gridx = 1;
        panel.add(idField, constraints);

        // Password
        JLabel passwordLabel = new JLabel("密码: ");
        constraints.gridx = 0;
        constraints.gridy = 1;
        panel.add(passwordLabel, constraints);

        passwordField = new JPasswordField(20);
        constraints.gridx = 1;
        panel.add(passwordField, constraints);

        // Login button
        JButton loginButton = new JButton("确认登录");
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        loginButton.setPreferredSize(new Dimension(100, 25)); // set preferred size
        panel.add(loginButton, constraints);

        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String id = idField.getText();
                String password = new String(passwordField.getPassword());

                File file = new File(System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "students.txt");
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(file));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        String[] parts = line.split(",");
                        if (parts.length >= 2) {
                            String fileID = parts[0];
                            String filePassword = parts[1];
                            if (id.equals(fileID) && password.equals(filePassword)) {
                                new MainPage().setVisible(true);
                                LoginPage.this.dispose();
                                return;
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(
                            LoginPage.this,
                            "错误的学号或者密码！",
                            "失败",
                            JOptionPane.ERROR_MESSAGE
                    );
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });

        // Register button
        JButton registerButton = new JButton("前往注册");
        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.CENTER;
        registerButton.setPreferredSize(new Dimension(100, 25)); // set preferred size
        panel.add(registerButton, constraints);

        registerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new RegisterPage().setVisible(true);
                LoginPage.this.dispose();
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LoginPage().setVisible(true);
            }
        });
    }
}
